package com.example.bishe.controller.bzks;

import com.alibaba.fastjson.JSON;
import com.example.bishe.entity.Bzks_bj;
import com.example.bishe.entity.Rygz;
import com.example.bishe.entity.Zsj_bzks;
import com.example.bishe.service.Bzks_bjService;
import com.example.bishe.service.RygzService;
import com.example.bishe.service.Zsj_bzksService;
import com.example.bishe.service.impl.Bzks_bjServiceImpl;
import com.example.bishe.service.impl.RygzServiceImpl;
import com.example.bishe.service.impl.Zsj_bzksServiceImpl;
import com.example.bishe.toolclass.CUD;
import com.example.bishe.toolclass.StringTurnJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author yws
 * @Date 2022/4/4 19:02
 */
@RestController
@RequestMapping("/bzks")
public class BzksController {
    @Resource
    StringTurnJson stringTurnJson;
    @Resource
    CUD cud;
    @Autowired
    Zsj_bzksService bzks = new Zsj_bzksServiceImpl();
    @Autowired
    Bzks_bjService bzks_bj = new Bzks_bjServiceImpl();
    @PostMapping("/addbzks")
    public String addbzks(@RequestBody Zsj_bzks zsj_bzks){
        System.out.println(zsj_bzks.toString());
        int errcode = 0;//错误代码1701
        String errmsg =" ";//错误描述
        HashMap value = new HashMap();
        Zsj_bzks z = new Zsj_bzks();
        z.setXh(zsj_bzks.getXh());
        Zsj_bzks[] zsjBzks = bzks.select_bzks(z);
        if(zsj_bzks.getXh()==null)
            errmsg+="请求中没有添加学生的学号信息，";
        if(zsj_bzks.getXm()==null)
            errmsg+="请求中没有添加学生的姓名信息，";
        if(zsj_bzks.getXbdm()==0)
            errmsg+="请求中没有添加学生的性别信息，";
        if(zsj_bzks.getCsrq()==null)
            errmsg+="请求中没有添加学生的出生日期信息，";
        if(zsj_bzks.getBh()==null)
            errmsg+="请求中没有添加学生的班级号码信息，";
        if(zsj_bzks.getYear()==null)
            errmsg+="请求中没有添加学生的毕业年限信息，";
        if(zsj_bzks.getSfzh()==null)
            errmsg+="请求中没有添加学生的身份证号信息，";
        if (zsj_bzks.getTelphone() != null) {
            String x = "1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\\d|9\\d)\\d{8}";
            if (!zsj_bzks.getTelphone().matches(x))
                errmsg += "手机号格式不对，请确定你的手机号是否正确，";
        }
        /*对应的院系和专业代码根据班级号码查询，然后自动添加进来，其中邮箱也是根据输入的学生的姓名进行生成的。*/
        if(errmsg.length()>1){
            errcode = 1701;
            errmsg = errmsg.substring(1, errmsg.length() - 1) + "。";
        }else if(zsjBzks.length==0){/*是否存在这个学生的学号*/
            Bzks_bj bzksbj = new Bzks_bj();
            bzksbj.setBh(zsj_bzks.getBh());
            Bzks_bj[] bzksbj1 = bzks_bj.select_bj(bzksbj);/*查询是否存在班级*/
            if(bzksbj1.length>0){/*存在班级信息*/
                bzksbj = bzksbj1[0];
                zsj_bzks.setDwdm(bzksbj.getYxdm());zsj_bzks.setZydm(bzksbj.getZydm());
                zsj_bzks.setXzdm(4);zsj_bzks.setZxzt(1);zsj_bzks.setMm("123456");
                bzks.insert_bzks(zsj_bzks);
                errmsg += "无错误。";
                errmsg = errmsg.substring(1, errmsg.length());
            }else {
                errcode = 1701;
                errmsg += "班级号码在不存在。";
            }
        }else {
            errcode = 1701;
            errmsg += "你所要加入的学生信息，学号已经存在。";
            errmsg = errmsg.substring(1, errmsg.length());
        }
        Map<String, String> map = new HashMap();
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        String s = JSON.toJSONString(map);
        System.out.println(s);
        return s;
    }
    @Resource
    RygzService rygzService = new RygzServiceImpl();
    @PostMapping("/deletebzks")
    public String deletebzks(@RequestBody Zsj_bzks zsj_bzks){
        int errcode = 0;//错误代码1702
        String errmsg ="";//错误描述
        HashMap<String, String> value = new HashMap();
        if(zsj_bzks.getXh()==null){
            errcode=1702;
            errmsg = "请传入你要删除的学生的学号。";
        }else {
            Zsj_bzks zsjBzks = new Zsj_bzks();zsjBzks.setXh(zsj_bzks.getXh());
            Zsj_bzks[] zsjBzks1 = bzks.select_bzks(zsjBzks);
            if(zsjBzks1.length!=0){
                zsjBzks = zsjBzks1[0];
                value = cud.deletestudent_jzg(zsjBzks.getXh());
                if (value.get("errcode").equals("0")||value.get("errcode").equals("60111")) {
                    Rygz rygz = new Rygz();
                    rygz.setUserid(zsj_bzks.getXh());
                    rygzService.deleteRygz(rygz);/*删除对应学生的关注信息*/
                    bzks.delete_bzks(zsjBzks);
                    errmsg = "已删除对应的学生信息。";
                } else {
                    errmsg+= cud.deletestudent_jzgpan(value);
                    errcode=1702;
                }
            }else {
                errcode=1702;
                errmsg = "没找到你要删除的学生信息。";
            }
        }
        Map map = new HashMap();
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        String s = JSON.toJSONString(map);
        return s;
    }
    @PostMapping("/updatebzks")
    public String updatebzks(@RequestBody Zsj_bzks zsj_bzks){
        int errcode = 0;//错误代码1703
        String errmsg ="";//错误描述
        HashMap<String, String> value = new HashMap();
        if(zsj_bzks.getTelphone()!=null){
            String x = "1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\\d|9\\d)\\d{8}";
            if(!zsj_bzks.getTelphone().matches(x)){
                errcode=1703;
                errmsg+="手机号格式不对，请确定你的手机号是否正确。";
            }
        }
        if(zsj_bzks.getXh()==null){
            errcode=1703;
            errmsg = "请最少传入你要修改的学生的学号。";
        }else if(errcode==0){
            Zsj_bzks zsjBzks = new Zsj_bzks();zsjBzks.setXh(zsj_bzks.getXh());
            Zsj_bzks[] zsjBzks1 = bzks.select_bzks(zsjBzks);
            if(zsjBzks1.length!=0){
                zsjBzks = zsjBzks1[0];
                String banhao="";
                if(zsj_bzks.getBh()!=null){
                    Bzks_bj bzksbj = new Bzks_bj();
                    bzksbj.setBh(zsj_bzks.getBh());
                    Bzks_bj[] bzks_bjs = bzks_bj.select_bj(bzksbj);
                    if(bzks_bjs.length!=0){
                        bzksbj = bzks_bjs[0];
                        banhao = bzksbj.getWx_id()+"";
                        value = cud.updatestudent_jzg(zsjBzks.getXh(),
                                zsj_bzks.getXm(),
                                zsj_bzks.getXbdm()+"",
                                zsj_bzks.getEmail(),
                                banhao,zsj_bzks.getTelphone());
                        if(value.get("errcode").equals("0")||value.get("errcode").equals("60111")){
                            errcode=0;
                            errmsg+="已修改对应学生信息";
                            /*应该修改其他的信息*/
                            Rygz rygz = new Rygz();
                            rygz.setUserid(zsj_bzks.getXh());
                            rygz.setUsername(zsj_bzks.getXm());
                            rygz.setPhone(zsjBzks.getTelphone());
                            rygzService.queryRygz(rygz);
                            zsj_bzks.setDwdm(bzksbj.getYxdm());
                            zsj_bzks.setZydm(bzksbj.getZydm());
                            bzks.query_bzks(zsj_bzks);
                        }else {
                            errcode=1703;
                            errmsg+= cud.updatestudent_jzgpan(value);
                        }
                    }else {
                        errcode=1703;
                        errmsg = "输入的班级编号不存在。";
                    }
                }else {
                    value = cud.updatestudent_jzg(zsj_bzks.getXh(), zsj_bzks.getXm(), zsj_bzks.getXbdm()+"",zsj_bzks.getEmail(),banhao,zsj_bzks.getTelphone());
                    bzks.query_bzks(zsj_bzks);
                }
            }else {
                errcode=1702;
                errmsg = "没找到你要修改的学生信息。";
            }
        }
        Map map = new HashMap();
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        String s = JSON.toJSONString(map);
        return s;
    }
    @PostMapping("/selectbzks")
    public String selectbzks(@RequestBody Zsj_bzks zsj_bzks){
        Zsj_bzks[] zsjBzks = bzks.select_bzks(zsj_bzks);
        List<Zsj_bzks> list = new ArrayList<>();
        for (int i = 0; i < zsjBzks.length; i++) {
            list.add(zsjBzks[i]);
        }
        Map map = new HashMap();
        map.put("Zsj_bzks",list);
        String s = JSON.toJSONString(map);
        return s;
    }
}
